package com.bw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.domain.Channel;
import com.bw.domain.Type;
import com.bw.service.ChannelService;
import com.bw.service.TypeService;
import com.bw.utils.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.swagger.models.auth.In;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * @author bobo棒
 * @date 2024年07月08日 15:34
 */

@RestController
@RequestMapping("channel2")
public class ChannelController2 {
    @Autowired
    ChannelService channelService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    TypeService typeService;

    @Autowired
    RedissonClient redissonClient;
    @Autowired
    RBloomFilter<String> bloomFilter;

    @PostMapping("list")
    public R list(@RequestBody Channel channel){

        //从数据局库获取
        List<Channel> list = channelService.list3(channel);
        //查询redis所有数据
        Set<ZSetOperations.TypedTuple> set = redisTemplate.opsForZSet().rangeWithScores(UP_CHANNEL_KEY, 0, Integer.MAX_VALUE);
        for (ZSetOperations.TypedTuple typedTuple : set) {
            Object channelId = typedTuple.getValue();
            Double upNumber = typedTuple.getScore();
            for (Channel channel1 : list) {
                //如果栏目id和redis中的栏目id一致
                System.out.println("channel1:="+channel1+"channelId:="+channelId);
                if(channel1.getId().toString().equals(channelId.toString())){
                    channel1.setUpNumber(upNumber.intValue());
                }
            }
        }

        return R.success(list);
    }


    @PostMapping("listType")
    public R listType() {
        List<Type> list = typeService.list();
        return R.success(list);
    }


    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("get/{id}")
    public R get(@PathVariable("id")Integer id) {
        Channel channel = channelService.getById(id);
        return R.success(channel);
    }


    //点赞用户key
    String UP_USER_KEY="UP_USER_KEY";
    //点赞栏目key
    String  UP_CHANNEL_KEY="UP_CHANNEL_KEY";
    /**
     * 点赞
     * @param id
     * @return
     */
    @PostMapping("up/{id}")
    public R up(@PathVariable("id")Integer id, HttpServletRequest request){
         //
        //获取用户id
        String token = request.getHeader("token");
        if(token==null ||token.equals("")){
            return R.error("请先登录");
        }
        Claims claims = Jwts.parser().setSigningKey("BW".getBytes()).parseClaimsJws(token).getBody();
        Integer userId = claims.get("id", Integer.class);

        //用户id+栏目id
        String USER_CHANNEL_KEY=userId+":"+id;

        String CHANNEL_KEY=id+"";

        Boolean b = redisTemplate.opsForSet().isMember(UP_USER_KEY, USER_CHANNEL_KEY);
        if(b){//如果点赞过.则取消点赞
            //让点赞数量-1
            redisTemplate.opsForZSet().incrementScore(UP_CHANNEL_KEY,CHANNEL_KEY,-1);
            //让点赞的人key删除
            redisTemplate.opsForSet().remove(UP_USER_KEY,USER_CHANNEL_KEY);
            return R.success("取消点赞成功",null);
        }

        //没有点赞过.则栏目点赞数量+1

        redisTemplate.opsForZSet().incrementScore(UP_CHANNEL_KEY,CHANNEL_KEY,1);
        //把点赞过的用户及栏目id存入 set
        redisTemplate.opsForSet().add(UP_USER_KEY,USER_CHANNEL_KEY);

        return R.success();
    }


}
